package com.flag.liutao.modules.role.controller;


import com.flag.liutao.common.domain.IdParam;
import com.flag.liutao.common.domain.IdsParam;
import com.flag.liutao.common.domain.ResponseResult;
import com.flag.liutao.modules.role.param.RoleParam;
import com.flag.liutao.modules.role.param.RoleQueryParam;
import com.flag.liutao.modules.role.service.IRoleService;
import com.flag.liutao.modules.role.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author godM
 * @since 2022-12-02
 */
@RestController
@RequestMapping("/admin/role")
@Api(tags = "角色管理")
@RequiredArgsConstructor
public class RoleController {

    public final IRoleService roleService;

    /**
     * 获取角色列表
     * @param param
     * @return
     */
    @GetMapping("page")
    @ApiOperation(value="获取角色列表",notes = "角色管理：获取角色列表")
    @PreAuthorize("hasAuthority('sys_power:role_R')")
    public ResponseResult<Map<String,Object>> getRoleList(RoleQueryParam param){
        Map<String,Object> map = roleService.getRolePage(param.getPage(),param.getLimit(),param.getKeyword());
        return ResponseResult.ok(map);
    }

    /**
     * 获取角色列表
     * @param
     * @return
     */
    @GetMapping("list")
    @ApiOperation(value="获取角色列表",notes = "角色管理：获取角色列表")
    @PreAuthorize("hasAuthority('sys_power:role_R')")
    public ResponseResult<List<RoleVo>> getRoleList(){
        List<RoleVo> list  = roleService.getRoleList();
        return ResponseResult.ok(list);
    }



    /**
     * 增加角色
     * @param param
     * @return
     */
    @PostMapping("add")
    @ApiOperation(value = "添加角色信息",notes = "角色管理:添加角色信息")
    @PreAuthorize("hasAuthority('sys_power:role_C')")
    public ResponseResult<Boolean> addRole(@Validated @RequestBody RoleParam param){
        roleService.addRole(param);
        return ResponseResult.ok();
    }

    /**
     * 修改角色
     * @param param
     * @return
     */
    @PostMapping("edit")
    @ApiOperation(value = "修改角色信息",notes = "角色管理:修改角色信息")
    @PreAuthorize("hasAuthority('sys_power:role_U')")
    public ResponseResult<Boolean> editRole(@Validated @RequestBody RoleParam param){
        roleService.EditRole(param);
        return ResponseResult.ok();
    }

    /**
     * 删除角色
     * @param param
     * @return
     */
    @PostMapping("del")
    @ApiOperation(value = "删除角色信息",notes = "角色管理:删除角色信息")
    @PreAuthorize("hasAuthority('sys_power:role_D')")
    public ResponseResult<Boolean> delRole(@Validated @RequestBody IdParam param){
        roleService.delRole(param.getId());
        return ResponseResult.ok();
    }


    /**
     * 批量删除角色
     * @param param
     * @return
     */
    @PostMapping("dels")
    @ApiOperation(value = "批量删除角色信息",notes = "角色管理:批量删除角色信息")
    @PreAuthorize("hasAuthority('sys_power:role_D')")
    public ResponseResult<Boolean> delsRole(@Validated @RequestBody IdsParam param){
        List<Long> ids = param.getIds();
        for (Long id : ids) {
            roleService.delRole(id);
        }
        return ResponseResult.ok();
    }

}
